﻿using System.ComponentModel;
using System.Collections;
using System.Web.UI;
using Triad.Controls;

namespace Triad.Controls
{
    /// <summary>
    /// Overrides the default ASP.Net ControlIDConverter class
    /// </summary>
    public class ControlIDConverter : StringConverter
    {
        /// <summary>
        /// Filters the control based on typed text.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <returns></returns>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 1:47 PM
        /// Modified By: 
        /// </remarks>
        protected virtual bool FilterControl(Control control)
        {
          if(control.GetType() == typeof (tfComboBox)) return true;
           return false;
        }

        /// <summary>
        /// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be null.</param>
        /// <returns>
        /// A <see cref="T:System.ComponentModel.TypeConverter.StandardValuesCollection"/> that holds a standard set of valid values, or null if the data type does not support a standard set of values.
        /// </returns>
        public override StandardValuesCollection GetStandardValues
                            (ITypeDescriptorContext context)
        {
            if (context == null)
                return null;

            IContainer container = context.Container;

            if (container == null)
                return null;

            ComponentCollection ctrlCollection = container.Components;
            ArrayList arrayList = new ArrayList(0);

            foreach (Control control in ctrlCollection)
            {
                if (FilterControl(control))
                    arrayList.Add(control.ID);
            }
            return new StandardValuesCollection(arrayList);
        }

        /// <summary>
        /// Returns whether the collection of standard values returned from <see cref="M:System.ComponentModel.TypeConverter.GetStandardValues"/> is an exclusive list of possible values, using the specified context.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
        /// <returns>
        /// true if the <see cref="T:System.ComponentModel.TypeConverter.StandardValuesCollection"/> returned from <see cref="M:System.ComponentModel.TypeConverter.GetStandardValues"/> is an exhaustive list of possible values; false if other values are possible.
        /// </returns>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }

        /// <summary>
        /// Returns whether this object supports a standard set of values that can be picked from a list, using the specified context.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
        /// <returns>
        /// true if <see cref="M:System.ComponentModel.TypeConverter.GetStandardValues"/> should be called to find a common set of values the object supports; otherwise, false.
        /// </returns>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            if (context == null) return false;
            return true;
        }
    }
}

